`timescale 1ns / 1ps
`include "defines.vh"
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2024/06/10 22:33:11
// Design Name: 
// Module Name: RiscV
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


// Risc-V处理器顶层模块
module RiscV(
    input [0:0] clk,
    input [0:0] rst,

    // 发送到最顶层
    output [`Reg_Data_Bus] Output_Top, 
    output [0:0]  Output_Top_Flag, 
    input [`Reg_Data_Bus]  Input_Top
    );

    //General   
    wire [0:0]Clear_flag = 1'b0;  //暂停标志 //-//未使用


    //PC
    wire [0:0]jump_flag = 1'b0;  //跳转标志位 //-//未使用
    wire [`Instruction_Addr_Bus]jump_addr = 32'b0;  //跳转地址 //-//未使用



    //PC to ROM
    wire [`Instruction_Addr_Bus]Instruction_addr_PC_to_ROM_and_IF_ID;  //目标指令地址


    //ROM to IF_ID to ID
    wire [`Instruction_Bus] Instruction_ROM_to_IF_ID;
    wire [`Instruction_Bus] Instruction_IF_ID_to_ID;
    wire [`Instruction_Addr_Bus] Instruction_Addr_IF_IF_to_ID;
 


    // assign Output_Top = Instruction_IF_ID_to_ID;


    //ID to Regs
    wire [`Reg_Addr_Bus] Read_Reg_1_Addr_ID_to_Regs;
    wire [`Reg_Addr_Bus] Read_Reg_2_Addr_ID_to_Regs;


    //ID to ID_EX to CU

    wire [`Instruction_Bus] Instruction_output_ID_to_ID_EX;  //指令内容
    wire [`Instruction_Addr_Bus] Instruction_Addr_output_ID_to_ID_EX;  //指令内容

    wire    [6:0] opcode_ID_to_ID_EX;  //操作码
    wire    [11:0] imm12_ID_to_ID_EX;  //12位立即数
    wire    [19:0] imm20_ID_to_ID_EX;  //20位立即数
    wire    [0:0] dis_ID_to_ID_EX;  //区分码
    wire    [2:0]func3_ID_to_ID_EX; //func3
    wire    [6:0] func7_ID_to_ID_EX; //func7

    wire    [0:0] Write_Reg_Flag_ID_to_ID_EX;  //寄存器写标志
    wire    [`Reg_Addr_Bus] Write_Reg_Addr_ID_to_ID_EX;  //寄存器写地址



    wire [`Instruction_Bus] Instruction_output_ID_EX_to_EX;  //指令内容
    wire [`Instruction_Addr_Bus] Instruction_Addr_output_ID_EX_to_EX;  //指令内容

    wire    [6:0] opcode_ID_EX_to_EX;  //操作码
    wire    [11:0] imm12_ID_EX_to_EX;  //12位立即数
    wire    [19:0] imm20_ID_EX_to_EX;  //20位立即数
    wire    [0:0] dis_ID_EX_to_EX;  //区分码
    wire    [2:0]func3_ID_EX_to_EX; //func3
    wire    [6:0] func7_ID_EX_to_EX; //func7

    wire    [0:0] Write_Reg_Flag_ID_EX_to_EX;  //寄存器写标志
    wire    [`Reg_Addr_Bus] Write_Reg_Addr_ID_EX_to_EX;  //寄存器写地址

    wire    [`Reg_Addr_Bus] Reg_1_Read_Addr_ID_EX_to_EX;
    wire    [`Reg_Addr_Bus] Reg_2_Read_Addr_ID_EX_to_EX;



    //Regs to ID_EX to CU
    wire [`Reg_Data_Bus] Reg_1_Data_Regs_to_ID_EX;
    wire [`Reg_Data_Bus] Reg_2_Data_Regs_to_ID_EX;

    wire [`Reg_Data_Bus] Reg_1_Data_ID_EX_to_CU;
    wire [`Reg_Data_Bus] Reg_2_Data_ID_EX_to_CU;

    //CU to ALU
    wire [`ALU_Operation_BUS] ALU_Operation;
    wire [`ALU_Parameter_BUS] ALU_A_Output;
    wire [`ALU_Parameter_BUS] ALU_B_Output;


    //CU to EX_MEM to MEM
    wire [0:0] Write_Reg_from_ALU_Flag_EX_to_EX_MEM;
    wire [0:0] Write_Reg_from_CU_Flag_EX_to_EX_MEM;
    wire [`Reg_Data_Bus] Write_Reg_from_CU_Data_EX_to_EX_MEM;

    wire [0:0] Write_Reg_Flag_EX_to_EX_MEM;
    wire [`Reg_Addr_Bus] Write_Reg_Addr_EX_to_EX_MEM;
    wire [0:0] RAM_Access_Flag_CU_to_EX_MEM;
    wire [`RAM_Operation_BUS] RAM_Operation_CU_to_EX_MEM;
    wire [`RAM_Data_BUS] RAM_Write_Data_CU_to_EX_MEM; 
    
    wire [0:0] Write_Reg_from_ALU_Flag_EX_MEM_to_MEM;
    wire [0:0] Write_Reg_from_CU_Flag_EX_MEM_to_MEM;
    wire [`Reg_Data_Bus] Write_Reg_from_CU_Data_EX_MEM_to_MEM;

    wire [0:0] Write_Reg_Flag_EX_MEM_to_MEM;
    wire [`Reg_Addr_Bus] Write_Reg_Addr_EX_MEM_to_MEM;
    wire [0:0] RAM_Access_Flag_EX_MEM_to_MEM;
    wire [`RAM_Operation_BUS] RAM_Operation_EX_MEM_to_MEM;
    wire [`RAM_Data_BUS] RAM_Write_Data_EX_MEM_to_MEM;


    //ALU to EX_MEM to MEM
    wire [`ALU_Parameter_BUS] ALU_Result_EX_to_EX_MEM;
    wire [ 0:0] ALU_Carry_EX_to_EX_MEM;
    wire [ 0:0] ALU_Overflow_EX_to_EX_MEM;

    wire [`ALU_Parameter_BUS] ALU_Result_EX_MEM_to_MEM;
    wire [ 0:0] ALU_Carry_EX_MEM_to_MEM;
    wire [ 0:0] ALU_Overflow_EX_MEM_to_MEM;

    //EX_MEM to ID_EX 前递操作
    wire [0:0] Write_Reg_Flag_Forward_EX_MEM_to_ID_EX;
    wire [`Reg_Addr_Bus] Write_Reg_Addr_Forward_EX_MEM_to_ID_EX;
    wire [`ALU_Parameter_BUS] ALU_Result_Forward_EX_MEM_to_ID_EX;




    ////MEM to MEM_WB to Regs
    wire [0:0] Write_Reg_Flag_MEM_to_MEM_WB;
    wire [`Reg_Addr_Bus] Write_Reg_Addr_MEM_to_MEM_WB;
    wire [`Reg_Data_Bus] Write_Reg_Data_MEM_to_MEM_WB;

    wire [0:0] Write_Reg_Flag_MEM_WB_to_Regs;
    wire [`Reg_Addr_Bus] Write_Reg_Addr_MEM_WB_to_Regs;
    wire [`Reg_Data_Bus] Write_Reg_Data_MEM_WB_to_Regs;

    //MEM_WB to ID_EX 前递操作
    wire [0:0] Write_Reg_Flag_MEM_WB_to_ID_EX;
    wire [`Reg_Addr_Bus] Write_Reg_Addr_MEM_WB_to_ID_EX;
    wire [`Reg_Data_Bus] Write_Reg_Data_MEM_WB_to_ID_EX;


    //JU 实现跳转
    wire [0:0] IF_ID_Clear_Flag;
    wire [0:0] ID_EX_Clear_Flag;
    wire [0:0] EX_MEM_Clear_Flag;

    wire [0:0] Jump_Flag;
    wire [`Instruction_Addr_Bus] Jump_Target_Inst_Addr;



    //// 实例化

    //PC模块实例化
    PC I_PC(
        .rst(rst),
        .clk(clk),
        .addr_Output(Instruction_addr_PC_to_ROM_and_IF_ID),

        .jump_flag_Input(Jump_Flag),
        .jump_addr_Input(Jump_Target_Inst_Addr),
        .Hold_flag_Input(Clear_flag)
    );


    //ROM模块实例化
    ROM I_ROM(
        .rst(rst),
        .clk(clk),

        .Instruction_addr_Input(Instruction_addr_PC_to_ROM_and_IF_ID),
        .Instruction_Output(Instruction_ROM_to_IF_ID),

        .Clear_flag_Input(Clear_flag)    
    );

    //IF_ID锁存器实例化
    IF_ID I_IF_ID(
        .rst(rst),
        .clk(clk),

        .Clear_Flag_Input(IF_ID_Clear_Flag),

        .Instruction_input(Instruction_ROM_to_IF_ID),
        .Instruction_Addr_input(Instruction_addr_PC_to_ROM_and_IF_ID),


        .Instruction_output(Instruction_IF_ID_to_ID),
        .Instruction_Addr_output(Instruction_Addr_IF_IF_to_ID)
    );


    //ID模块实例化
    ID I_ID(
        .rst(rst),
        .clk(clk),

        .Instruction_Input(Instruction_IF_ID_to_ID),
        .Instruction_Addr_Input(Instruction_Addr_IF_IF_to_ID),

        .Read_Reg_1_Addr_Output(Read_Reg_1_Addr_ID_to_Regs),
        .Read_Reg_2_Addr_Output(Read_Reg_2_Addr_ID_to_Regs),


        .Instruction_Output(Instruction_output_ID_to_ID_EX),
        .Instruction_Addr_Output(Instruction_Addr_output_ID_to_ID_EX),

        .opcode(opcode_ID_to_ID_EX),
        .imm12(imm12_ID_to_ID_EX),
        .imm20(imm20_ID_to_ID_EX),
        .dis(dis_ID_to_ID_EX),
        .func3(func3_ID_to_ID_EX),
        .func7(func7_ID_to_ID_EX),

        .Write_Reg_Flag(Write_Reg_Flag_ID_to_ID_EX),
        .Write_Reg_Addr(Write_Reg_Addr_ID_to_ID_EX)
    );

    ID_EX I_ID_EX(
        .rst(rst),
        .clk(clk),

        .Clear_Flag_Input(ID_EX_Clear_Flag),
    
        .Instruction_Input(Instruction_output_ID_to_ID_EX),  //指令内容
        .Instruction_Addr_Input(Instruction_Addr_output_ID_to_ID_EX),

        .opcode_Input(opcode_ID_to_ID_EX), //操作码
        .imm12_Input(imm12_ID_to_ID_EX), //12位立即数
        .imm20_Input(imm20_ID_to_ID_EX),//20位立即数
        .dis_Input(dis_ID_to_ID_EX),  //区分码
        .func3_Input(func3_ID_to_ID_EX), //func3
        .func7_Input(func7_ID_to_ID_EX),//func7
    
        .Reg_1_Read_Data_Input(Reg_1_Data_Regs_to_ID_EX),
        .Reg_2_Read_Data_Input(Reg_2_Data_Regs_to_ID_EX),

        .Write_Reg_Flag_Input(Write_Reg_Flag_ID_to_ID_EX),  //寄存器写标志
        .Write_Reg_Addr_Input(Write_Reg_Addr_ID_to_ID_EX),  //寄存器写地址
    
    
        .Instruction_Output(Instruction_output_ID_EX_to_EX),  //指令内容
        .Instruction_Addr_Output(Instruction_Addr_output_ID_EX_to_EX),

        .opcode_Output(opcode_ID_EX_to_EX), //操作码
        .imm12_Output(imm12_ID_EX_to_EX), //12位立即数
        .imm20_Output(imm20_ID_EX_to_EX),//20位立即数
        .dis_Output(dis_ID_EX_to_EX),  //区分码
        .func3_Output(func3_ID_EX_to_EX), //func3
        .func7_Output(func7_ID_EX_to_EX),//func7
    
        .Reg_1_Read_Data_Output(Reg_1_Data_ID_EX_to_CU),
        .Reg_2_Read_Data_Output(Reg_2_Data_ID_EX_to_CU),

        .Write_Reg_Flag_Output(Write_Reg_Flag_ID_EX_to_EX),  //寄存器写标志
        .Write_Reg_Addr_Output(Write_Reg_Addr_ID_EX_to_EX),  //寄存器写地址

        .Reg_1_Read_Addr_Output(Reg_1_Read_Addr_ID_EX_to_EX),
        .Reg_2_Read_Addr_Output(Reg_2_Read_Addr_ID_EX_to_EX),


        .Write_Reg_Flag_Forward_Input(Write_Reg_Flag_Forward_EX_MEM_to_ID_EX),
        .Write_Reg_Addr_Forward_Input(Write_Reg_Addr_Forward_EX_MEM_to_ID_EX),
        .ALU_Result_Forward_Input(ALU_Result_Forward_EX_MEM_to_ID_EX),


        .Write_Reg_Flag_Forward_Input2(Write_Reg_Flag_MEM_WB_to_ID_EX),
        .Write_Reg_Addr_Forward_Input2(Write_Reg_Addr_MEM_WB_to_ID_EX),
        .Write_Reg_Data_Forward_Input2(Write_Reg_Data_MEM_WB_to_ID_EX),


        .Reg_1_Read_Addr_Input(Read_Reg_1_Addr_ID_to_Regs),
        .Reg_2_Read_Addr_Input(Read_Reg_2_Addr_ID_to_Regs)

        


    );






    //Regs模块实例化
    Regs I_Regs(
        .rst(rst),
        .clk(clk),

        .Read_Reg_1_Addr_Input(Read_Reg_1_Addr_ID_to_Regs),
        .Read_Reg_1_Data_Output(Reg_1_Data_Regs_to_ID_EX),
        .Read_Reg_2_Addr_Input(Read_Reg_2_Addr_ID_to_Regs),
        .Read_Reg_2_Data_Output(Reg_2_Data_Regs_to_ID_EX),


        .Write_Reg_Flag_Input(Write_Reg_Flag_MEM_WB_to_Regs),
        .Write_Reg_Addr_Input(Write_Reg_Addr_MEM_WB_to_Regs),
        .Write_Reg_Data_Input(Write_Reg_Data_MEM_WB_to_Regs)
    );
    

    //CU模块实例化
    CU I_CU(
        .rst(rst),
        .clk(clk),
        

        .Instruction_Input(Instruction_output_ID_EX_to_EX),
        .Instruction_Addr_Input(Instruction_Addr_output_ID_EX_to_EX),

        .opcode(opcode_ID_EX_to_EX),
        .imm12(imm12_ID_EX_to_EX),
        .imm20(imm20_ID_EX_to_EX),
        .dis(dis_ID_EX_to_EX),
        .func3(func3_ID_EX_to_EX),
        .func7(func7_ID_EX_to_EX),

        .Write_Reg_Flag(Write_Reg_Flag_ID_EX_to_EX),
        .Write_Reg_Addr(Write_Reg_Addr_ID_EX_to_EX),

        
        .ALU_Operation(ALU_Operation),
        .ALU_A_Output(ALU_A_Output),
        .ALU_B_Output(ALU_B_Output),

        .Reg_1_Read_Data_Input(Reg_1_Data_ID_EX_to_CU),
        .Reg_2_Read_Data_Input(Reg_2_Data_ID_EX_to_CU),

        .Write_Reg_Flag_Output(Write_Reg_Flag_EX_to_EX_MEM),
        .Write_Reg_Addr_Output(Write_Reg_Addr_EX_to_EX_MEM),
        .RAM_Access_Flag_Output(RAM_Access_Flag_CU_to_EX_MEM),
        .RAM_Operation_Output(RAM_Operation_CU_to_EX_MEM),
        .RAM_Write_Data_Output(RAM_Write_Data_CU_to_EX_MEM),

        .Write_Reg_from_ALU_Flag_Output(Write_Reg_from_ALU_Flag_EX_to_EX_MEM),
        .Write_Reg_from_CU_Flag_Output(Write_Reg_from_CU_Flag_EX_to_EX_MEM),
        .Write_Reg_from_CU_Data_Output(Write_Reg_from_CU_Data_EX_to_EX_MEM),

        .Input_Top(Input_Top),
        .Output_Top(Output_Top),
        .Output_Top_Flag(Output_Top_Flag)
    );


    JU I_JU(
        .clk(clk),
        .rst(rst),

        .Instruction_Input(Instruction_output_ID_EX_to_EX),
        .Instruction_Addr_Input(Instruction_Addr_output_ID_EX_to_EX),

        .Reg_1_Read_Addr_Input(Reg_1_Read_Addr_ID_EX_to_EX),
        .Reg_2_Read_Addr_Input(Reg_2_Read_Addr_ID_EX_to_EX),


        .RAM_Access_Flag_Input(RAM_Access_Flag_EX_MEM_to_MEM),
        .RAM_Operation_Input(RAM_Operation_EX_MEM_to_MEM),


        .Write_Reg_Addr_Input(Write_Reg_Addr_EX_MEM_to_MEM),

        .ALU_Result_Input(ALU_Result_EX_to_EX_MEM),
        .ALU_Carry_Output(ALU_Carry_EX_to_EX_MEM),
        .ALU_Overflow_Output(ALU_Overflow_EX_to_EX_MEM),

        .opcode(opcode_ID_EX_to_EX),
        .func3(func3_ID_EX_to_EX),


        .IF_ID_Clear_Flag(IF_ID_Clear_Flag),
        .ID_EX_Clear_Flag(ID_EX_Clear_Flag),
        .EX_MEM_Clear_Flag(EX_MEM_Clear_Flag),

        .Jump_Flag(Jump_Flag),
        .Jump_Target_Inst_Addr(Jump_Target_Inst_Addr)


    );


















    //ALU模块实例化
    ALU I_ALU(
        .rst(rst),
        .clk(clk),

        .Operation(ALU_Operation),
        .A_Input(ALU_A_Output),
        .B_Input(ALU_B_Output),
        .Result_Output(ALU_Result_EX_to_EX_MEM),
        .Carry_Output(ALU_Carry_EX_to_EX_MEM),
        .Overflow_Output(ALU_Overflow_EX_to_EX_MEM)

    );


    //EX_MEM实例化
    EX_MEM I_EX_MEM(
        .rst(rst),
        .clk(clk),

        .Clear_Flag_Input(EX_MEM_Clear_Flag),

        .ALU_Result_Input(ALU_Result_EX_to_EX_MEM),
        .ALU_Carry_Input(ALU_Carry_EX_to_EX_MEM),
        .ALU_Overflow_Input(ALU_Overflow_EX_to_EX_MEM),

        .Write_Reg_from_ALU_Flag_Input(Write_Reg_from_ALU_Flag_EX_to_EX_MEM),
        .Write_Reg_from_CU_Flag_Input(Write_Reg_from_CU_Flag_EX_to_EX_MEM),
        .Write_Reg_from_CU_Data_Input(Write_Reg_from_CU_Data_EX_to_EX_MEM),

        .Write_Reg_Flag_Input(Write_Reg_Flag_EX_to_EX_MEM),
        .Write_Reg_Addr_Input(Write_Reg_Addr_EX_to_EX_MEM),

        .RAM_Access_Flag_Input(RAM_Access_Flag_CU_to_EX_MEM),
        .RAM_Operation_Input(RAM_Operation_CU_to_EX_MEM),
        .RAM_Write_Data_Input(RAM_Write_Data_CU_to_EX_MEM),

        .ALU_Result_Output(ALU_Result_EX_MEM_to_MEM),
        .ALU_Carry_Output(ALU_Carry_EX_MEM_to_MEM),
        .ALU_Overflow_Output(ALU_Overflow_EX_MEM_to_MEM),

        .Write_Reg_from_ALU_Flag_Output(Write_Reg_from_ALU_Flag_EX_MEM_to_MEM),
        .Write_Reg_from_CU_Flag_Output(Write_Reg_from_CU_Flag_EX_MEM_to_MEM),
        .Write_Reg_from_CU_Data_Output(Write_Reg_from_CU_Data_EX_MEM_to_MEM),

        .Write_Reg_Flag_Output(Write_Reg_Flag_EX_MEM_to_MEM),
        .Write_Reg_Addr_Output(Write_Reg_Addr_EX_MEM_to_MEM),

        .RAM_Access_Flag_Output(RAM_Access_Flag_EX_MEM_to_MEM),
        .RAM_Operation_Output(RAM_Operation_EX_MEM_to_MEM),
        .RAM_Write_Data_Output(RAM_Write_Data_EX_MEM_to_MEM),



        .Write_Reg_Flag_Forward_Output(Write_Reg_Flag_Forward_EX_MEM_to_ID_EX),
        .Write_Reg_Addr_Forward_Output(Write_Reg_Addr_Forward_EX_MEM_to_ID_EX),
        .ALU_Result_Forward_Output(ALU_Result_Forward_EX_MEM_to_ID_EX)


    );


    MEM I_MEM(
        .rst(rst),
        .clk(clk),

        .ALU_Result(ALU_Result_EX_MEM_to_MEM),
        .ALU_Carry(ALU_Carry_EX_MEM_to_MEM),
        .ALU_Overflow(ALU_Overflow_EX_MEM_to_MEM),


        .Write_Reg_from_ALU_Flag(Write_Reg_from_ALU_Flag_EX_MEM_to_MEM),
        .Write_Reg_from_CU_Flag(Write_Reg_from_CU_Flag_EX_MEM_to_MEM),
        .Write_Reg_from_CU_Data(Write_Reg_from_CU_Data_EX_MEM_to_MEM),

        .Write_Reg_Flag_Input(Write_Reg_Flag_EX_MEM_to_MEM),
        .Write_Reg_Addr_Input(Write_Reg_Addr_EX_MEM_to_MEM),

        .RAM_Access_Flag_Input(RAM_Access_Flag_EX_MEM_to_MEM),
        .RAM_Operation_Input(RAM_Operation_EX_MEM_to_MEM),
        .RAM_Write_Data_Input(RAM_Write_Data_EX_MEM_to_MEM),

        .Write_Reg_Flag_Output(Write_Reg_Flag_MEM_to_MEM_WB),
        .Write_Reg_Addr_Output(Write_Reg_Addr_MEM_to_MEM_WB),
        .Write_Reg_Data_Output(Write_Reg_Data_MEM_to_MEM_WB)

    );


    MEM_WB I_MEM_WB(
        .rst(rst),
        .clk(clk),

        .Write_Reg_Flag_Input(Write_Reg_Flag_MEM_to_MEM_WB),
        .Write_Reg_Addr_Input(Write_Reg_Addr_MEM_to_MEM_WB),
        .Write_Reg_Data_Input(Write_Reg_Data_MEM_to_MEM_WB),

        .Write_Reg_Flag_Output(Write_Reg_Flag_MEM_WB_to_Regs),
        .Write_Reg_Addr_Output(Write_Reg_Addr_MEM_WB_to_Regs),
        .Write_Reg_Data_Output(Write_Reg_Data_MEM_WB_to_Regs),

        .Write_Reg_Flag_Forward_Output(Write_Reg_Flag_MEM_WB_to_ID_EX),
        .Write_Reg_Addr_Forward_Output(Write_Reg_Addr_MEM_WB_to_ID_EX),
        .Write_Reg_Data_Forward_Output(Write_Reg_Data_MEM_WB_to_ID_EX)


    );










endmodule
